import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '@/components/Login.vue'
// import Home from '@/components/Home.vue'
// import Welcome from '@/components/Welcome.vue'
const Login = () => import(/* webpackChunkName: "login-home-welcome" */ '@/components/Login.vue')
const Home = () => import(/* webpackChunkName: "login-home-welcome" */ '@/components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login-home-welcome" */ '@/components/Welcome.vue')

// import Users from '@/components/user/Users.vue'
// import Rights from '@/components/power/Rights.vue'
// import Roles from '@/components/power/Roles.vue'
const Users = () => import(/* webpackChunkName: "user-rights-roles" */ '@/components/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "user-rights-roles" */ '@/components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "user-rights-roles" */ '@/components/power/Roles.vue')

// import GoodsCate from '@/components/goods/Cate.vue'
// import GoodsParams from '@/components/goods/Params.vue'
// import GoodsList from '@/components/goods/List.vue'
// import GoodsAdd from '@/components/goods/Add.vue'
const GoodsCate = () => import(/* webpackChunkName: "goods" */ '@/components/goods/Cate.vue')
const GoodsParams = () => import(/* webpackChunkName: "user-rights-roles" */ '@/components/goods/Params.vue')
const GoodsList = () => import(/* webpackChunkName: "user-rights-roles" */ '@/components/goods/List.vue')
const GoodsAdd = () => import(/* webpackChunkName: "user-rights-roles" */ '@/components/goods/Add.vue')

// import Order from '@/components/order/Orders.vue'
// import Reports from '@/components/report/Reports.vue'
const Order = () => import(/* webpackChunkName: "order-report" */ '@/components/order/Orders.vue')
const Reports = () => import(/* webpackChunkName: "order-report" */ '@/components/report/Reports.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: GoodsCate },
      { path: '/params', component: GoodsParams },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: GoodsAdd },
      { path: '/orders', component: Order },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // 获取 Token 字符串
  const tokenStr = window.sessionStorage.getItem('token')
  // 判断请求的是否为登录页面
  if (to.path === '/login') {
    if (!tokenStr) return next()
    return router.push('/home')
  }
  // 请求的不是登录页面
  if (!tokenStr) return next('/login')
  next()
})

export default router
